package com.webfinal.backsystem.service.impl;

import com.webfinal.backsystem.dao.CarouselMapper;
import com.webfinal.backsystem.entity.Carousel;
import com.webfinal.backsystem.request.carousel.AddCarouselRequest;
import com.webfinal.backsystem.request.carousel.UpdateCarouselRequest;
import com.webfinal.backsystem.request.product.AddProductRequest;
import com.webfinal.backsystem.response.carousel.AddCarouselResponse;
import com.webfinal.backsystem.response.carousel.GetCarouselResponse;
import com.webfinal.backsystem.response.carousel.UpdateCarouselReponse;
import com.webfinal.backsystem.service.CarouselService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CarouselServiceImpl implements CarouselService {
    @Autowired
    private CarouselMapper carouselMapper;
    @Override
    public GetCarouselResponse getCarousel() {
        List<Carousel> carouselList=carouselMapper.selectByDate(new Date());
        GetCarouselResponse response = new GetCarouselResponse();
        response.setResultCode("0");
        response.setCarouselList(carouselList);
        return response;
    }

    @Override
    public AddCarouselResponse addCarousel(AddCarouselRequest addCarouselRequest) {
        AddCarouselResponse response = new AddCarouselResponse();
        Carousel carousel = new Carousel();
        carousel.setCarouselId(0);
        carousel.setProductId(addCarouselRequest.getProductId());
        carousel.setProductImage(addCarouselRequest.getProductImage());
        carousel.setStartDate(addCarouselRequest.getStartDate());
        carousel.setEndDate(addCarouselRequest.getEndDate());
        if(carouselMapper.insert(carousel)==1){
            response.setResultCode("0");
            response.setResultMsg("success");
        }else {
            response.setResultCode("1");
            response.setResultMsg("error");
        }
        return response;
    }

    @Override
    public UpdateCarouselReponse updateCarousel(UpdateCarouselRequest updateCarouselRequest) {
        UpdateCarouselReponse response = new UpdateCarouselReponse();
        Carousel carousel = carouselMapper.selectByPrimaryKey(updateCarouselRequest.getCarouselId());
        carousel.setProductId(updateCarouselRequest.getProductId());
        carousel.setProductImage(updateCarouselRequest.getProductImage());
        carousel.setStartDate(updateCarouselRequest.getStartDate());
        carousel.setEndDate(updateCarouselRequest.getEndDate());
        if(carouselMapper.updateByPrimaryKey(carousel)==1){
            response.setResultCode("0");
            response.setResultMsg("success");
        }else {
            response.setResultCode("1");
            response.setResultMsg("error");
        }
        return response;
    }
}
